package com.wgts.wgts.mvp.view.activity.goodsinfo;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.base.LazyBaseFragment;
import com.wgts.wgts.dagger.ComponentHolder;
import com.wgts.wgts.dagger.component.DaggerGoodProductEvaluateFragmentComponent;
import com.wgts.wgts.dagger.module.GoodProductEvaluateFragmentModule;
import com.wgts.wgts.mvp.presenter.IGoodProductEvaluateFragmentPersenter;
import com.wgts.wgts.mvp.view.IGoodProductEvaluateFragmentView;
import com.wgts.wgts.netmodle.resp.ProductEvaluateEntity;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class ProductEvaluateFragment extends LazyBaseFragment implements IGoodProductEvaluateFragmentView {
    @BindView(R.id.recycle_view)
    RecyclerView recycleView;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;
    Unbinder unbinder;
    @BindView(R.id.tv_praise)
    TextView tvPraise;
    @BindView(R.id.relative_evaluate)
    RelativeLayout relativeEvaluate;
    @BindView(R.id.none_data_ly)
    LinearLayout noneDataLy;
    @BindView(R.id.net_error_ly)
    LinearLayout netErrorLy;

    private LinearLayoutManager manager;

    private ProductEvaluateAdapter adapter;

    private List<ProductEvaluateEntity.Evaluate> evaluateEntitys;


    @Inject
    IGoodProductEvaluateFragmentPersenter persenter;

    private static final String PRODUCT_ID = "PRODUCT_ID";

    @Override
    protected void loadData() {

    }

    @Override
    public void init() {
        manager = new LinearLayoutManager(getContext());
        //manager.setOrientation(LinearLayoutManager.VERTICAL);

        DaggerGoodProductEvaluateFragmentComponent
                .builder()
                .appComponent(ComponentHolder.getInstance().getAppComponent())
                .goodProductEvaluateFragmentModule(new GoodProductEvaluateFragmentModule(this))
                .build()
                .inject(this);

        evaluateEntitys = new ArrayList<>();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.product_evaluate_fragment, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        return view;
    }

    private void initView() {
        recycleView.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new ProductEvaluateAdapter(getContext(), evaluateEntitys);
        recycleView.setAdapter(adapter);
        persenter.initProductEvaluate(getArguments().getString(PRODUCT_ID));
        refreshLayout.setEnableRefresh(false);
        refreshLayout.setEnableLoadmore(true);

        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                persenter.productEvaluate();
            }
        });

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void evaluateResult(ProductEvaluateEntity evaluateEntity, boolean noMore) {

        adapter.setNoMore(noMore);

        if (noMore) {
            refreshLayout.setEnableLoadmore(false);
        } else {
            refreshLayout.setEnableLoadmore(true);
        }

        refreshLayout.finishLoadmore();

        if (evaluateEntity.list == null || evaluateEntity.list.size() == 0) {
            relativeEvaluate.setVisibility(View.GONE);
        } else {
            relativeEvaluate.setVisibility(View.VISIBLE);
        }

        evaluateEntitys.addAll(evaluateEntity.list);

        adapter.notifyDataSetChanged();


    }

    @Override
    public void initEvaluateResult(ProductEvaluateEntity evaluateEntity, boolean noMore) {

        if(evaluateEntity.list.size() == 0){
            noneDataLy.setVisibility(View.VISIBLE);
        }


        tvPraise.setText(evaluateEntity.rate);

        if (evaluateEntity.list == null || evaluateEntity.list.size() == 0) {
            relativeEvaluate.setVisibility(View.GONE);
        } else {
            relativeEvaluate.setVisibility(View.VISIBLE);
        }

        evaluateEntitys.clear();

        adapter.setNoMore(noMore);
        if (noMore) {
            refreshLayout.setEnableLoadmore(false);
        } else {
            refreshLayout.setEnableLoadmore(true);
        }


        evaluateEntitys.addAll(evaluateEntity.list);
        adapter.notifyDataSetChanged();


    }

    @Override
    public void onError(int status, String msg) {
        recycleView.setVisibility(View.GONE);
        netErrorLy.setVisibility(View.VISIBLE);
    }

    public static ProductEvaluateFragment getInstance(String id) {
        ProductEvaluateFragment fragment = new ProductEvaluateFragment();
        Bundle bundle = new Bundle();
        bundle.putString(PRODUCT_ID, id);
        fragment.setArguments(bundle);
        return fragment;
    }
}
